package net.oschina.aibinxiao.wechatauth.util;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @Author: AiBinXiao
 * @Description: 根据接口地址进行网络请求
 * @Date: 2017/10/17 9:51
 */

public class AuthUtil {
    public static final String APPID = "wxf845957f70e0210d";
    public static final String APPSECRET = "1966ba1cf8f5dd8fcb34e9add6e67efa";

    public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException{
        JSONObject jsonObject = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = client.execute(httpGet); // 得到响应结果
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSONObject.fromObject(result); // 将响应结果转换为json对象
        }
        httpGet.releaseConnection(); // 释放连接
        return jsonObject;
    }
}
